"use client";

import { Database } from '@/types_db';
import { useState } from 'react';
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs';
import { SessionContextProvider } from '@supabase/auth-helpers-react';

interface SupabaseProviderProps {
  children: React.ReactNode;
}



const SupabaseProvider: React.FC<SupabaseProviderProps> = ({ children }) => {
  // 身份验证可以在客户端或服务器端发起。Auth Helpers 客户端支持所有supabase-js 身份验证策略。
  //客户端组件可用于从事件处理程序触发身份验证过程。
  const [supabaseClient] = useState(() => createClientComponentClient<Database>());
  /**
   console.log(supabaseClient);
   * {
      "supabaseUrl": "https://xxxxxxxx.supabase.co",
      "supabaseKey": "xxxxxxxx.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
      "realtimeUrl": "wss://xxxxxxxx.supabase.co/realtime/v1",
      "authUrl": "https://xxxxxxxx.supabase.co/auth/v1",
      "storageUrl": "https://xxxxxxxx.supabase.co/storage/v1",
      "functionsUrl": "https://xxxxxxxx.supabase.co/functions/v1",
      "storageKey": "sb-xxxxxxxx-auth-token",
      "headers": {
          "X-Client-Info": "@supabase/auth-helpers-nextjs@0.7.0"
      },
      "auth": {
          "memoryStorage": null,
          "stateChangeEmitters": {},
          "autoRefreshTicker": 13,
          "refreshingDeferred": null,
          "initializePromise": {},
          "detectSessionInUrl": true,
          "lockAcquired": false,
          "pendingInLock": [],
          "broadcastChannel": {},
          "instanceID": 0,
          "logDebugMessages": false,
          "persistSession": true,
          "storageKey": "sb-xtulaumgejdftfxrmljq-auth-token",
          "autoRefreshToken": true,
          "admin": {
              "url": "https://xxx.supabase.co/auth/v1",
              "headers": {
                  "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
                  "apikey": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
                  "X-Client-Info": "@supabase/auth-helpers-nextjs@0.7.0"
              },
              "mfa": {}
          },
          "url": "https://xxxxxxx.supabase.co/auth/v1",
          "headers": {
              "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
              "apikey": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
              "X-Client-Info": "@supabase/auth-helpers-nextjs@0.7.0"
          },
          "flowType": "pkce",
          "mfa": {},
          "storage": {
              "cookieOptions": {
                  "path": "/",
                  "maxAge": 31536000000
              }
          }
      },
      "realtime": {
          "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM",
          "channels": [],
          "endPoint": "wss://xxxxxxx.supabase.co/realtime/v1/websocket",
          "headers": {
              "X-Client-Info": "@supabase/auth-helpers-nextjs@0.7.0"
          },
          "params": {
              "apikey": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0dWxhdW1nZWpkZnRmeHJtbGpxIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTczNDkyOTMsImV4cCI6MjAxMjkyNTI5M30.wO-Zh4MSSDeApaZeOldndtUCb3oQNe0bZz6Pps51ZwM"
          },
          "timeout": 10000,
          "heartbeatIntervalMs": 30000,
          "pendingHeartbeatRef": null,
          "ref": 0,
          "conn": null,
          "sendBuffer": [],
          "serializer": {
              "HEADER_LENGTH": 1
          },
          "stateChangeCallbacks": {
              "open": [],
              "close": [],
              "error": [],
              "message": []
          },
          "eventsPerSecondLimitMs": 100,
          "inThrottle": false,
          "reconnectTimer": {
              "tries": 0
          }
      },
      "rest": {
          "url": "https://xxxxxxx.supabase.co/rest/v1",
          "headers": {
              "X-Client-Info": "@supabase/auth-helpers-nextjs@0.7.0"
          },
          "schemaName": "public"
      }
  }
   */
  

  return ( 
    <SessionContextProvider supabaseClient={supabaseClient}>
      {children}
    </SessionContextProvider>
  );
}

export default SupabaseProvider;